package com.cultusoft.groovin.plugins.tomcat;

import com.cultusoft.groovin.utils.SystemProperties;
import com.cultusoft.groovin.log.GroovinLogger

class TomcatServerStopper {

    def logger = new GroovinLogger(this)
	
 	public void run() {
 		logger.log "Shutting down Tomcat"
 		stopTomcatServer()
 		waitForTomcatToShutdown()
 		logger.log("Tomcat has shutdown")
 	}
 	
 	def waitForTomcatToShutdown() {
 		new TomcatShutdownVerifier().run()
 	}
 	private void stopTomcatServer() {
 		def systemProperties = new SystemProperties()
 		if (systemProperties.isLinux()) {
			stopTomcatServerInLinux(); 			
 		} else {
 			stopTomcatServerInWindows()
 		}
 	}
 	
 	
 	private void stopTomcatServerInWindows() {
 		def process = ("cmd /c " + System.getenv("CATALINA_HOME") + "\\bin\\shutdown.bat").execute()
 		process.in.eachLine { line -> logger.log(line) }
 	}
 	
 	private void stopTomcatServerInLinux() {
 		def process = (System.getenv("CATALINA_HOME") + "/bin/shutdown.sh").execute()
 		process.in.eachLine { line -> logger.log(line) }
 	}

}
